<?
 require_once("users.php");
 require_once("../email/email.php");
 require_once("../phone/phone.php");
 require_once("../address/address.php");
 
 if(isset($_POST['edit']))
 {
 	if($_POST['edit'] === '1')
	{
		$results = updateUsers($_POST);

		header('location:list.php');
	}
 }
 

 $address_results = getAddresses($_GET['id']);

 $phone_results = getUserPhones($_GET['id']);
 
 $email_results = getUserEmails($_GET['id']);

 $results = getUsersInfo($_GET['id']);

 $row = $results->fetch_assoc();
 
?>
<!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8" />
        <title><?=$title ?> Edit Customer: <?=$row['id'] ?></title>
         <? require('../inc/header-info.php'); ?>
    </head>
    <body>
        <div class="container">
            <? include('../inc/nav.php'); ?>
             <form name="form1" method="post" action="edit.php">
             	<h4>Edit Customer: <?=$row['id']?></h4>
             	<input type="hidden" name="edit" value='1'> 
            	<input type="hidden" name="id" value='<?=$row['id'] ?>'> 					
    			
					<label class="control-label" for="first_name">First Name:</label>
					<div class="controls">
						<input type="text" id="first_name"  name="first_name" placeholder="First Name" value='<?=htmlentities($row['first_name'])?>'>
					</div>
				
				
					<label class="control-label" for="middle_name">Middle Name:</label>
					<div class="controls">
						<input type="text" id="middle_name" name="middle_name" placeholder="Middle Name" value='<?=htmlentities($row['middle_name'])?>'>
					</div>
				
					<label class="control-label" for="last_name">Last Name:</label>
					<div class="controls">
						<input type="text" id="last_name" name="last_name" placeholder="Last Name" value='<?=htmlentities($row['last_name'])?>'>
					</div>
    		
            	<div class="form-actions">
  					<button type="submit" class="btn btn-primary">Save changes</button>
					<a href="<?=$url?>employees/index.php"><button class="btn btn-primary" type="button">Cancel</button></a>
				</div>	
            </form>
            
			<h4>Email Contacts:</h4>
    		<h5>New Email</h5>
   	  		<form class="form-inline"  name="newEmail" method="post" action="../email/addemail.php">
  				<div class="input-append">
  				<input type="hidden" name="CreateEmail" value='1'>
  			    <input type="hidden" name="id" value='<?=$_GET['id']?>'>
    			<input type="email" name="newEmail" class="span3" placeholder="New Email">
    			<select name='contact_type' class="span2">
  					<option value='20'>Personal Email</option>
  					<option value='19'>Work Email</option>
				</select>
    			<button type="submit" class="btn">New Email</button>
  				</div>
  			</form>
          
            
            <table class="table table-striped">
    			<thead>
    				<tr>
    					<th>Email Address</th>
    					<th>Type</th>
    					<th>Options</th>
    				</tr>
    			</thead>
    			<tbody>
    				
    				<? while($email_row = $email_results->fetch_assoc()): ?>
    					<tr>
							<td><?=htmlentities($email_row['address'])?></td>
							<td><?=htmlentities($email_row['Contact'])?></td>
							<td>
								<a href="<?=$url?>email/addemail.php?id=<?=$row['id'] ?>&email_id=<?=$email_row['id']?>"><button class="btn btn-mini btn-primary" type="button">Edit</button></a>
                            	<a href="<?=$url?>email/email-edit.php?id=<?=$row['id'] ?>&email_id=<?=$email_row['id']?>"><button class="btn btn-mini btn-primary" type="button">Delete</button></a>
							</td>
                        </tr>
                    <? endwhile; ?>

    			</tbody>
    		</table>
  			<h4>Phone Contacts:</h4>
  			<h5>New Phone</h5>
   	  		<form class="form-inline"  name="newPhone" method="post" action="../phone/phone-edit.php">
  				<div class="input-append">
  				<input type="hidden" name="CreatePhone" value='1'>
  			    <input type="hidden" name="id" value='<?=$_GET['id']?>'>
    			<input type="text" name="newPhone" class="span3" placeholder="New Phone">
    			<select name='contact_type' class="span2">
  					<option value='17'>Home Phone</option>
  					<option value='25'>Work Phone</option>
  					<option value='24'>Mobile Phone</option>
				</select>
    			<button type="submit" class="btn">New Phone</button>
  				</div>
  			</form>
            <table class="table table-striped">
    			<thead>
    				<tr>
    					<th>Phone Number</th>
    					<th>Type</th>
    					<th>Options</th>
    				</tr>
    			</thead>
    			<tbody>
    				
    				<? while($phone_row = $phone_results->fetch_assoc()): ?>
    					<tr>
							<td><?=htmlentities($phone_row['number'])?></td>
							<td><?=htmlentities($phone_row['Contact'])?></td>
							<td>
								<a href="<?=$url?>phone/phone-update.php?id=<?=$row['id'] ?>&addressid=<?=$phone_row['id']?>"><button class="btn btn-mini btn-primary" type="button">Edit</button></a>
                            	<a href="<?=$url?>phone/phone-edit.php?id=<?=$row['id'] ?>&addressid=<?=$phone_row['id']?>"><button class="btn btn-mini btn-primary" type="button">Delete</button></a>
							</td>
                        </tr>
                    <? endwhile; ?>
                    
    			</tbody>
    		</table>
    		<h4>Customer Address:</h4>
    		<a href="<?=$url?>address/create.php?id=<?=$_GET['id'] ?>"><button class="btn btn-mini btn-primary" type="button">Add Address</button></a>
    		<table class="table table-striped">
    			<thead>
    				<tr>
    					<th>Type</th>
    					<th>Address</th>
    					<th></th>
    					<th>City</th>
    					<th>State</th>
    					<th>Country</th>
    					<th>Postal Code</th>
    					<th>Options</th>
    				</tr>
    			</thead>
    			<tbody>

    				<? while($address_row = $address_results->fetch_assoc()): ?>
    					<tr>
							<td><?=htmlentities($address_row['Contact_type'])?></td>
    						<td><?=htmlentities($address_row['address0'])?></td>
    						<td><?=htmlentities($address_row['address1'])?></td>
    						<td><?=htmlentities($address_row['city'])?></td>
    						<td><?=htmlentities($address_row['state'])?></td>
    						<td><?=htmlentities($address_row['country'])?></td>
    						<td><?=htmlentities($address_row['postal_code'])?></td>
							<td>
								<a href="<?=$url?>address/address-update.php?id=<?=$row['id'] ?>&addressid=<?=$address_row['id']?>"><button class="btn btn-mini btn-primary" type="button">Edit</button></a>
                            	<a href="<?=$url?>address/address-edit.php?id=<?=$row['id'] ?>&addressid=<?=$address_row['id']?>"><button class="btn btn-mini btn-primary" type="button">Delete</button></a>
							</td>
                        </tr>
                    <? endwhile; ?>

    			</tbody>
    		</table>
		<br />
		<br />	
		</div>

	</body>
</html>